#define  _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//排查坐标周围有几个雷
static int get_mine_count(char board[ROWS][COLS], int i, int j)
{
	return board[i][j - 1] + board[i][j + 1] + board[i + 1][j] + board[i + 1][j - 1] + board[i + 1][j + 1]
		+ board[i - 1][j - 1] + board[i - 1][j] + board[i - 1][j + 1] - 8 * '0';
}

//初始化数组
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

//打印数组
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("--------------扫雷游戏--------------\n");
	for (j = 0; j <= col; j++)
	{
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("--------------扫雷游戏--------------\n");
}

//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = EAS;
	while (count)
	{
		int i = rand() % 9 + 1;
		int j = rand() % 9 + 1;
		if (board[i][j] == '0')
		{
			board[i][j] = '1';
			count--;
		}
	}
}

//排查雷
void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	int win = 0;
	while(win < row * col-EAS)
	{
		printf("请输入要排查的坐标-->");
		scanf("%d %d", &i, &j);

		//判断输入坐标的合法性
		if (i >= 1 && i <= 9 && j >= 1 && j <= 9)
		{
			if (mine[i][j] == '1')
			{
				printf("很遗憾，你被炸死了");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int count = get_mine_count(mine, i, j);
				show[i][j] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("输入错误，请重新输入");
		}
	}
	if (win == row * col - EAS)
	{
		printf("恭喜你，排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}